home *** CD-ROM | disk | FTP | other *** search
- #include "stdio.h"
- #include "macCdefs.h"
- #include "Window.h"
- #include "Packages.h"
- #include "PBDefs.h"
- #include "OSIo.h"
-
-
- struct F_BLK{ // note everything is char to avoid alignment padding
- char zero1;
- char nlen;
- char name[63];
- char type[4];
- char creator[4];
- char flags;
- char zero2;
- char location[6];
- char protected;
- char zero3;
- char dflen[4];
- char rflen[4];
- char cdate[4];
- char mdate[4];
- };
-
- struct F_BLK INFO_BLOCK,*F_INFO;
-
- char *fname,*srcname;
- char sector[128],BINARY,THISFORK;
- short secnum,vrefnum,infile,outfile;
- short Rblocks,Dblocks,BLOCKS;
- long dlen,rlen;
- SFTypeList types;
- SFReply RP;
- FileParam FP;
-
- _GetFileInfo(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- { CtoPstr((char *)iop->ioNamePtr = (char *)strsave(name));
- iop->ioVRefNum=vol;
- iop->ioFVersNum=iop->ioFDirIndex=0;
- PBGetFInfo(iop,0);
- free(iop->ioNamePtr);
- }
-
- _SetFileInfo(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- { CtoPstr((char *)iop->ioNamePtr = (char *)strsave(name));
- iop->ioVRefNum=vol;
- iop->ioFVersNum=iop->ioFDirIndex=0;
- PBSetFInfo(iop,0);
- free(iop->ioNamePtr);
- }
-
- MakeTextFile(vol,name,iop)
- short vol;
- char *name;
- FileParam *iop;
- { _GetFileInfo(vol,name,iop);
- iop->ioFlFndrInfo.fdType='TEXT';
- iop->ioFlFndrInfo.fdCreator='EDIT';
- _SetFileInfo(vol,name,iop);
- }
-
- _pack(f,t)
- char *f,*t;
- { BlockMove(f,t,4);
- }
-
- _open(file,acmode)
- char *file,acmode;
- { return(TKopen(diskdevice,file,acmode));
- }
-
- _read(file,sector,size)
- { clearbytes(sector,size);
- return(read(file,sector,size));
- }
-
- static int NumOfBlks(num)
- long num;
- { int blks=num/128;
- if(num%128) ++blks;
- return(blks);
- }
-
- static char IsBinary(f)
- struct F_BLK *f;
- { BINARY=((f->zero1 == 0) && (f->zero2 == 0) && (f->zero3 == 0));
- }
-
- static WriteBlk()
- { if(secnum == 0)
- { IsBinary(sector);
- if(BINARY) InitBinSave();
- else signal("Not a Binary File.");
- return
- }
- WriteBinary();
- }
-
- static InitBinSave()
- { BlockMove(sector,&INFO_BLOCK,sizeof(INFO_BLOCK));
- _pack(&INFO_BLOCK.rflen[0],&rlen);
- _pack(&INFO_BLOCK.dflen[0],&dlen);
- Rblocks=NumOfBlks(rlen);
- Dblocks=NumOfBlks(dlen);
- if(Dblocks != 0)
- { THISFORK = 0;
- BLOCKS=Dblocks;
- }
- else
- { THISFORK = 0xf0;
- BLOCKS=Rblocks;
- }
- if(!NewFileName(&INFO_BLOCK.nlen,&RP)) signal("Request Aborted.");
- vrefnum=MakeName(&RP,fname);
- if(OSFind(fname)) OSDelete(fname);
- outfile=_open(fname,THISFORK+2);
- }
-
-
- static _truncate(len)
- long len;
- { setbytepos(outfile,len);
- seteof(outfile);
- }
-
- static WriteBinary()
- { if(BLOCKS)
- { write(outfile,sector,128);
- --BLOCKS;
- }
- if(BLOCKS) return;
- if(THISFORK == 0)
- { BLOCKS=Rblocks;
- THISFORK = 0xf0;
- _truncate(dlen);
- TKclose(outfile);
- if(BLOCKS) outfile=_open(fname,0xf2);
- else outfile=0;
- }
- else _truncate(rlen);
- }
-
- static FinishFile()
- {
- _GetFileInfo(vrefnum,fname,&FP);
- BlockMove(&(INFO_BLOCK.type[0]),&(FP.ioFlFndrInfo),sizeof(FInfo));
- FP.ioFlFndrInfo.fdFlags &= 0xfeff; // clr init bit
- _pack(&(INFO_BLOCK.cdate[0]),&FP.ioFlCrDat);
- _pack(&(INFO_BLOCK.mdate[0]),&FP.ioFlMdDat);
- _SetFileInfo(vrefnum,fname,&FP);
- }
-
-
- App2Bin()
- { if(!OldFileName(-1,0,&RP)) return;
- MakeName(&RP,srcname);
- clearbytes(F_INFO=(struct F_BLK *)sector,128);
- BlockMove(&RP.Namelength,fname,64);
- BlockMove(&RP.Namelength,&F_INFO->nlen,64);
- PtoCstr(fname);
- strcat(fname,".bin");
- CtoPstr(fname);
- if(!NewFileName(fname,&RP)) return;
- vrefnum=MakeName(&RP,fname);
- if(OSFind(fname)) OSDelete(fname);
- if((infile=_open(srcname,1)) == 0)
- signal("Unable to read the file.");
- if((outfile=_open(fname,2)) == 0)
- signal("Unable to create the file.");
- _GetFileInfo(vrefnum,srcname,&FP);
- BlockMove(&FP.ioFlFndrInfo,&(F_INFO->type[0]),sizeof(FInfo));
- F_INFO->protected = (F_INFO->zero2 & 0x40)?1:0;
- F_INFO->zero2=0;
- _pack(&FP.ioFlLgLen,&(F_INFO->dflen[0]));
- _pack(&FP.ioFlRLgLen,&(F_INFO->rflen[0]));
- _pack(&FP.ioFlCrDat,&(F_INFO->cdate[0]));
- _pack(&FP.ioFlMdDat,&(F_INFO->mdate[0]));
- write(outfile,sector,128);
- while(_read(infile,sector,128)>0) write(outfile,sector,128);
- TKclose(infile);
- infile=_open(srcname,0xf1); // Open the resource fork now
- while(_read(infile,sector,128)>0) write(outfile,sector,128);
- TKclose(infile);
- TKclose(outfile);
- MakeTextFile(vrefnum,fname,&FP);
- }
-
- Bin2App()
- { types.ftype[0]='TEXT';
- if(!OldFileName(1,&types,&RP)) return;
- MakeName(&RP,srcname);
- if((infile=_open(srcname,1)) == 0) signal("Unable to read the file.");
- secnum=0;
- while(_read(infile,sector,128))
- { WriteBlk();
- ++secnum;
- }
- TKclose(infile);
- TKclose(outfile);
- FinishFile();
- }
-
- DoDialog(id,msg)
- short id;
- char *msg;
- { char *oldPort,*dialog,*msgp;
- short Hit;
- GetPort(&oldPort);
- SetPort(dialog=GetNewDialog(id,0,-1));
- if(msg)
- { MoveTo(20,10);
- DrawString(msgp=strsave(msg));
- free(msgp);
- }
- ModalDialog(0,&Hit);
- DisposeDialog(dialog);
- SetPort(oldPort);
- return(Hit);
- }
-
- main()
- { char *msg,*CatchSignal();
- InitDialogs(0);
- InitCursor();
- fname=malloc(256);
- srcname=malloc(256);
- if(msg=CatchSignal())
- { DoDialog(1,msg);
- }
- Again:
- switch(DoDialog(2,0))
- { case 2: App2Bin(); break;
- case 3: Bin2App(); break;
- case 4:
- { free(fname);
- free(srcname);
- exit();
- }
- }
- goto Again;
- }
-
-
-